package com.sip.comadapter.executor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sip.comadapter.common.BaseExecutor;
import com.sip.comadapter.executor.common.Executor;
import com.sip.comserver.communication.CommunicationMessage;
import com.sip.dbadapter.dao.domain.ApiDefinition;
import com.sip.dbadapter.dao.domain.ScenarioRecord;
import com.sip.dbadapter.dao.interfaces.ApiDao;
import com.sip.dbadapter.dao.interfaces.ScenarioRecordSaveDao;
import com.sip.dbadapter.dao.utility.DaoLoadFactory;
import com.sip.dbadapter.dao.utility.DaoNameConstant;
import com.sip.dbadapter.domain.registry.DomainRegistry;
import com.sip.executionadapter.manager.JerseyExecutionManager;

public class RecordedRestServicesExecutor extends BaseExecutor implements Executor {

    private static final Logger logger = Logger.getLogger(RecordedRestServicesExecutor.class);
    public CommunicationMessage execute(final CommunicationMessage message) throws Exception {

        logger.info("<<<<<<<< Inside RecordedRestServicesExecutor >>>>>>>>> ");

        final JerseyExecutionManager jerseyManager = JerseyExecutionManager.createInstance();
        final ScenarioRecordSaveDao scenarioRecordeDao = (ScenarioRecordSaveDao) DaoLoadFactory.getDao(DaoNameConstant.SCENARIO_RECORD_DAO);
        final ApiDao apiDao = (ApiDao) DaoLoadFactory.getDao(DaoNameConstant.API_DAO);
        
        Map<String, Object> domainMap;
        final List<ScenarioRecord> scenarioRecordList = (List<ScenarioRecord>) message.getObject();
        List<ScenarioRecord> scenarioRecordObjectList= new ArrayList<ScenarioRecord>();
        logger.info("==========scenarioRecordList Size=========> "+scenarioRecordList.size());
       
      
        for (ScenarioRecord scenarioRecordDomainObj : scenarioRecordList) {
            
            logger.info("<Inside RecordedRestServicesExecutor===inside LOOP=>"+scenarioRecordDomainObj.getApiDefinitionScenarioRecord().getApiId());
            domainMap=new HashMap<String, Object>();
            Long apiId=scenarioRecordDomainObj.getApiDefinitionScenarioRecord().getApiId();
            final ApiDefinition apiDefinition = (ApiDefinition) apiDao.findById(apiId);
            domainMap.put(DomainRegistry.API_DEFINITION, apiDefinition);
            scenarioRecordDomainObj.setRecordId(null);
            domainMap.put(DomainRegistry.SCENARIO_RECORD, scenarioRecordDomainObj);
        
            //invoke
            jerseyManager.invoke(domainMap);
           
            //save Scenario Record Object
            scenarioRecordDomainObj.setApiDefinitionScenarioRecord(apiDefinition);
            scenarioRecordDomainObj.setExecuteMode("BATCH");
            scenarioRecordeDao.save(scenarioRecordDomainObj);
            
            scenarioRecordObjectList.add(scenarioRecordDomainObj);
            
        
        
        }

        message.setObject(scenarioRecordObjectList);
        return message;
      
    }



}
